<?php namespace sacvoyage\domain;



use \sacvoyage\storage\Collection as StorageCollection;

use \sacvoyage\domain\Resource;

class Collection {
	
	public function __construct(User $owner, 
	                            StorageCollection $collection,
	                            $name,
	                            $permission='full') {
	    $this->_owner=$owner;
		$this->_collection=$collection;
		$this->_name=$name;
		$this->_permission=$permission;
	}
	
	public function getOwner() {
		return $this->_owner;
	}	
	
	public function getName() {
		return $this->_name;
	}

	public function write($documents) {
		if(in_array($this->_permission, array('full', 'write'))) {
			$this->getOwner()->getDomain()->getHookManager()->onWrite($this, $documents);
			return $this->_collection->write($documents);
		} else {
			throw new Exception(
				'You have no permission to write to this collection'
			);
		}
	}
	
	public function read($id, $rev=null){
		if(in_array($this->_permission, array('full', 'read'))) {
			return $this->_collection->read($id, $rev);
		} else {
			throw new Exception(
				'You have no permission to read from this collection'
			);
		}
	}
	
	public function find($query=null) {
		if(in_array($this->_permission, array('full', 'read'))) {
			return $this->_collection->find($query);
		} else {
			throw new Exception(
				'You have no permission to read from this collection'
			);
		}
	}
	
	public function delete($query) {
		if(in_array($this->_permission, array('full', 'write'))) {
			$this->getOwner()->getDomain()->getHookManager()->onDelete($this, $query);
			return $this->_collection->delete($query);
		} else {
			throw new Exception(
				'You have no permission to write to this collection'
			);
		}
	}
	
	public function purgeHistory() {
		$this->_collection->purgeHistory();
	}
	
	private $_owner;
	private $_collection;
	private $_name;
	private $_permission;
}

?>
